package com.starry.home.ui.adapter

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.starry.core.ext.singleClick
import com.starry.core.utils.GlideUtils
import com.starry.home.R
import com.starry.home.databinding.ItemSectionChildBinding
import com.starry.home.logic.model.hospital.Ys
import com.starry.home.ui.hospital.DoctorActivity

/**
 * Package:com.starry.dotpen_patients.ui.adapter
 * Author:starr
 * Time:2024/1/6 19:49
 * Description: 医生信息列表
 */
class SectionChildAdapter(
    private val sections: ArrayList<Ys?>,
) : RecyclerView.Adapter<SectionChildAdapter.SectionChildViewHolder>() {

    inner class SectionChildViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    @SuppressLint("NotifyDataSetChanged")
    fun updateData(data: ArrayList<Ys?>) {
        sections.clear()
        sections.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SectionChildViewHolder {
        val binding = DataBindingUtil.inflate<ItemSectionChildBinding>(
            LayoutInflater.from(parent.context), R.layout.item_section_child,
            parent, false
        )
        return SectionChildViewHolder(binding.root)
    }

    override fun getItemCount() = sections.size

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onBindViewHolder(holder: SectionChildViewHolder, position: Int) {
        val binding = DataBindingUtil.getBinding<ItemSectionChildBinding>(holder.itemView)
        binding?.let {
            it.tvYsName.text = sections[position]?.emrYsName
            GlideUtils.loadCircleImage(it.root.context, sections[position]?.emrYsImg, it.ivYsImg)
            it.tvYsName.singleClick {
                val bundle = Bundle()
                bundle.putInt("ysId", sections[position]?.emrYsId!!)
                DoctorActivity.startAction(holder.itemView.context, bundle)
            }
        }
    }
}